﻿using BySoft.Server.Business;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Enum;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.MasteData
{
    public partial class MaterialSmallTypeAdd : AuthBasePage
    {
        public string sequenceName
        {
            get { return "MD_MaterialSmallType"; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // txtMaterialSmallTypeID.Attributes.Add("readonly", "true");
                InitDDL();
            }
            Master.ValidationGroup = "gSubmit";
        }

        void InitDDL()
        {
            string[] strUnlikes = { "1" };
            DataTable dt = MdMaterialtype.GetMdMaterialtypeAll().Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                this.ddlPartType.DataSource = dt;
                this.ddlPartType.DataTextField = "MaterialTypeName";
                this.ddlPartType.DataValueField = "MaterialTypeID";
                this.ddlPartType.DataBind();
            }
            UIHelper.InsertFirstItem(ddlPartType);


            SetDDlView(true);

        }

        void SetDDlView(bool bView)
        {

            txtMaterialSmallTypeID.Text = "";
            txtMaterialSmallTypeName.Text = "";
        }






        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(CurrentUser.UserID))
                {
                    UIHelper.AlertMessage(this.Page, "等待时间太久，请重新填写原料小类资料！");
                    return;
                }
                if (string.IsNullOrEmpty(this.txtMaterialSmallTypeID.Text.Trim()))
                {
                    UIHelper.AlertMessage(this.Page, "原料小类编号不能为空，请填写！");
                    return;
                }
                if ((new MdMaterialsmalltype()).IsIDExists(txtMaterialSmallTypeID.Text.Trim()))
                {
                    UIHelper.AlertMessage(this.Page, "原料小类编号已存在，请重新填写！");
                    return;
                }
                if ((new MdMaterialsmalltype()).IsNameExists(this.txtMaterialSmallTypeName.Text.Trim(), ""))
                {
                    UIHelper.AlertMessage(this.Page, "原料小类名称已存在，请重新填写！");
                    return;
                }
                MdMaterialsmalltypeEntity entity = new MdMaterialsmalltypeEntity();
                entity.MaterialSmallTypeID = this.txtMaterialSmallTypeID.Text.Trim();
                entity.MaterialSmallTypeName = this.txtMaterialSmallTypeName.Text.Trim();
                entity.MaterialTypeID = ddlPartType.SelectedValue.Trim();

                entity.Status = "1";
                entity.CreateDate = DateTime.Now;
                entity.CreateUserID = CurrentUser.UserID;
                entity.CreateUserName = CurrentUser.UserName;

                if (MdMaterialsmalltype.InsertMdMaterialsmalltype(entity))
                {
                    base.LogAction(BaseEnum.ActionType.Add, entity.MaterialSmallTypeID, true, null, new EntityBase[] { entity });
                    UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "../MasteData/MaterialSmallTypeList.aspx");
                }
                else
                {
                    UIHelper.AlertMessage(this.Page, Message.SaveFail);
                }


            }
            catch (Exception ex)
            {
                UIHelper.AlertMessage(this.Page, FomatString(ex.Message.ToString()));
            }
        }
    }
}